﻿
using System;
using System.Collections;
using System.Collections.Generic;
using webapp4U.DAL;
using System.IO;
using System.Web;

namespace webapp4U.BOL
{
    public class File
    {
        #region "======Properties====="

        public static int PageIndex;// luu hinh hien tai
        private int _recordID; // chi lien quan den viec view datalist phan trang
        private int _fileID;
        private int _cateID;
        private string _fileName;
        private int _width;
        private int _height;
        private int _fileSize;
        private string _title;
        private string _description;
        private int _totalViews;
        private DateTime _postDate;
        private string _username;

        public int RecordID
        {
            get { return _recordID; }
            set { _recordID = value; }
        }


        public int FileID
        {
            get { return _fileID; }
            set { _fileID = value; }
        }

        public int CateID
        {
            get { return _cateID; }
            set { _cateID = value; }
        }

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public int FileSize
        {
            get { return _fileSize; }
            set { _fileSize = value; }
        }


        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        
        public int TotalViews
        {
            get { return _totalViews; }
            set { _totalViews = value; }
        }


        public DateTime PostDate
        {
            get { return _postDate; }
            set { _postDate = value; }
        }


        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        #endregion


        #region "==== Method ===="

        public File()
        {

        }


        // dem so file theo category
        public static int GetFileCountByCateID(int CateID)
        {
            FileDAL db = new FileDAL();
            return db.GetFileCountByCateID(CateID);
        }


        // lay duong dan cua file
        public static string GetPathByFileID(int FileID, PhotoSize Size)
        {
            FileDAL db = new FileDAL();
            string path = System.Web.HttpContext.Current.Server.MapPath("~/Gallery/");
            path = path + db.GetAppKeyByFileID(FileID);
            
            File obj = db.GetFileByFileID(FileID);

            if (obj.FileName != null && obj.FileName != "")
            {
                if (Size == PhotoSize.Small)
                    path = path + "/Small/" + obj.FileName;
                else
                    path = path + "/Large/" + obj.FileName;
            }
            else
                path = "";

            return path;
        }


        public static List<File> GetFileSlideShowByCateID(int CateID)
        {
            FileDAL db = new FileDAL();
            return db.GetFileSlideShowByCateID(CateID);
        }

          // lay file theo Category, co phan trang
        public static List<File> GetFileByCateID_Paging(int CateID, int PageIndex, int PageSize)
        {
            FileDAL db = new FileDAL();
            return db.GetFileByCateID_Paging(CateID, PageIndex, PageSize);
        }


        // lay file theo Category
        public static List<File> GetFileByCateID(int CateID)
        {
            FileDAL db = new FileDAL();
            return db.GetFileByCateID(CateID);
        }


        // lay file theo 1 CateID ngau nhien 
        public static List<File> GetFileRandomCateID()
        {
            FileDAL db = new FileDAL();
            // chi lay cac CateID thuoc loai Album Public
            return db.GetFileByCateID(db.GetRandomCateIDByParentCateID((int)eCategory.AlbumsPublic));
        }


        

        public static File GetFileByFileID(int FileID)
        {
            FileDAL db = new FileDAL();
            return db.GetFileByFileID(FileID);
        }      


        public static string GetDefaultPhoto(PhotoSize size)
        {
            string path = HttpContext.Current.Server.MapPath("~/Images/");
            switch (size)
            {
                case PhotoSize.Small:
                    path += "placeholder-100.jpg";
                    break;
                case PhotoSize.Medium:
                    path += "placeholder-200.jpg";
                    break;
                case PhotoSize.Large:
                    path += "placeholder-600.jpg";
                    break;
                default:
                    path += "placeholder-600.jpg";
                    break;
            }
            return path;
            //return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        }


        public static ICollection ListUploadDirectory()
        {
            DirectoryInfo d = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/Upload"));
            return d.GetFileSystemInfos("*.jpg");
        }


        public static int Insert(File obj)
        {
            FileDAL db = new FileDAL();
            return db.Insert(obj);
        }


        public static int Delete(int FileID)
        {
            try
            {
                webapp4U.BOL.File obj = webapp4U.BOL.File.GetFileByFileID(FileID);
                FileProcess.DeleteFile(Globals.PhysicalApplicationPath() + "/UserUpload/Gallery/" + obj.FileName.Trim());

                FileDAL db = new FileDAL();
                return db.Delete(FileID);
            }
            catch
            {
                return -1;
            }
        }

        public int Update(int FileID, string Title)
        {
            FileDAL db = new FileDAL();
            return db.Update(FileID, Title);
        }

        #endregion


    }
}
